﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.IO;

namespace skydream
{
    /// <summary>
    /// txt文件读取
    /// </summary>
    public class TxtReader
    {
        //
        // 摘要:
        //     读取txt文件的
        //    
        //      
        // 返回结果:
        //     返回ArrayList;
        /// <summary>
        /// 读取txt文件的
        /// </summary>
        /// <param name="txtName"></param>
        /// <returns></returns>
        public static ArrayList getTxt(string txtName){
            ArrayList ast=new ArrayList();
            //读第一个文件
            StreamReader read1 = null; //read2;
            FileStream fs = null;
            try
            {
                string s1;
                fs = new FileStream(@txtName, FileMode.Open);
                read1 = new StreamReader(fs);//1文件读取
                s1 = read1.ReadLine();
                while (s1 != null)
                {
                    ast.Add(s1);//存储到数列中
                    s1 = read1.ReadLine();
                }
            }
            catch (Exception ee)
            {

            }
            finally
            {
                read1.Close();
                read1.Dispose();
                fs.Dispose();
                fs.Close();
            }

            return ast;

        }
        //
        // 摘要:
        //     读取txt文件的
        //    
        //      
        // 返回结果:
        //     返回ArrayList;
        /// <summary>
        /// 读取txt文件的
        /// </summary>
        /// <param name="txtName"></param>
        /// <returns></returns>
        public static ArrayList getCETxt(string txtName)
        {
            ArrayList ast = new ArrayList();
            //读第一个文件
            StreamReader read1 = null; //read2;
            FileStream fs = null;
            try
            {
                string s1;
                fs = new FileStream(@txtName, FileMode.Open);
                read1 = new StreamReader(fs);//1文件读取
                s1 = read1.ReadLine();
                while (s1 != null)
                {
                    ast.Add(s1);//存储到数列中
                    s1 = read1.ReadLine();
                }
            }
            catch (Exception ee)
            {

            }
            finally
            {
                read1.Close();
                fs.Close();
            }

            return ast;

        }
        //
        // 摘要:
        //     写txt文件的一行
        //    
        //      
        // 返回结果:
        //     返回ArrayList;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="filePath"></param>
        /// <param name="lineStr"></param>
        public static void writeTxt(string filePath,string lineStr)
        {

            //读第一个文件
            StreamWriter sw = null; //read2;
            FileStream fs = null;
            try
            {
                fs = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.Read);
                fs.Seek(0, SeekOrigin.End);
                sw = new StreamWriter(fs, Encoding.Unicode);
                sw.WriteLine(lineStr);
            }
            catch (Exception ee)
            {

            }
            finally
            {
                sw.Close();
                fs.Close();
            }

        }

        //
        // 摘要:
        //     写txt文件的一行
        //    
        //      
        // 返回结果:
        //     返回ArrayList;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="filePath"></param>
        /// <param name="lineStr"></param>
        public static void writeTxt(string filePath, string lineStr,Encoding endcode)
        {

            //读第一个文件
            StreamWriter sw = null; //read2;
            FileStream fs = null;
            try
            {
                fs = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.Read);
                fs.Seek(0, SeekOrigin.End);

                sw = new StreamWriter(fs, endcode);
                sw.WriteLine(lineStr);
            }
            catch (Exception ee)
            {

            }
            finally
            {
                sw.Close();
                fs.Close();
            }

        }
        
    }
}
